package handler

import (
	"github.com/gin-gonic/gin"
	"house/api/request"
	"house/api/resp"
	"house/internal/dao"
	"house/internal/model"
)

func ListOrder(c *gin.Context) {
	var req request.ListOrder
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 100000, "验证失败", err.Error())
		return
	}
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if user.TypeUser != 3 {
		resp.Error(c, 100000, "您没有管理员权限", false)
		return
	}
	var order []model.Order
	result := dao.GetListByFields(&model.Order{}, &order)
	if !result {
		resp.Error(c, 100000, "订单查询失败", false)
		return
	}
	resp.Success(c, "订单查询成功", order)
}

func UpdateOrder(c *gin.Context) {
	var req request.UpdateOrder
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 100000, "验证失败", err.Error())
		return
	}
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if user.TypeUser != 3 {
		resp.Error(c, 100000, "您没有管理员权限", false)
		return
	}
	var order model.Order
	dao.GetOneById(req.Id, &order)
	if order.Id == 0 {
		resp.Error(c, 100000, "该订单不存在", false)
		return
	}
	result := dao.Update(&model.Order{
		Id:     req.Id,
		Status: req.Status,
	})
	if !result {
		resp.Error(c, 100000, "管理员修改失败", false)
		return
	}
	resp.Success(c, "管理员修改成功", true)
}
